const {
    QueryTypes
} = require('sequelize');
const Houses = require('../models/houses');
const {
    sequelize
} = require('../models/houses')
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
exports.getHouses = async function (pageNum,pageSize) {
    if(!pageNum ||! pageSize) {
        const result = await sequelize.query("SELECT * FROM `house`", {
            type: QueryTypes.SELECT,
        });
    };
    console.log(pageNum,pageSize);
    const result = await Houses.findAndCountAll( {
        type: QueryTypes.SELECT,
        limit:parseInt(pageSize),
        offset:parseInt((pageNum-1) * pageSize),
        raw:true
    });
    console.log(result);
    if (result) {
        return {
            data: {
                result
            },
            meta: {
                msg: '获取数据成功',
                status: '200'
            }
        }
    } else {
        return {
            data: {},
            meta: {
                msg: '获取数据失败',
                status: '400'
            }
        }
    }
}
exports.getHousesById = async function (id) {
    // console.log(id);
    const result = await sequelize.query("SELECT * FROM `house` where id = :id",
        {
            replacements: {
                id
            },
            type: QueryTypes.SELECT
        },

    );
    console.log(result);
    if (result) { return { data: result, meta: { msg: '获取数据成功', status: '200' } } }
    return { data: {}, meta: { msg: '获取数据失败', status: '400' } }

}
exports.addHouses = async function (description, price, floor, areaId, houseCode) {
    let result = await Houses.create({
        // where: {
            description,
            price,
            floor,
            areaId,
            houseCode
        // },
    });


    if (result) {
        return {

            data: {
                result
            },

            meta: {
                msg: '添加房源成功',
                status: '200'
            }
        }
    } else {
        return {
            data: {},
            meta: {
                msg: '添加房源失败',
                status: '400'
            }
        }
    }
}

exports.deleteHouses = async function (id) {
    const result = await Houses.destroy({
        where: {
            id,
        },
    }).catch(()=>{});
    return {
        data: {result},
        meta: {
            msg: '删除成功',
            status: '200'
        }
    }
};

exports.updateHouses = async function (description, price, floor, areaId, houseCode,id) {
    // console.log(description, price, floor, areaId, houseCode,id);
    const result = await Houses.update({description, price, floor, areaId ,houseCode},{
        where:{
            id 
        }                  
    }      
    ).catch(() => {});
    console.log(result);
    if(result){
        return {
        data: result,
        meta: {
            msg: '更新成功',
            status: '200'
        }
    }
    }
    
};

exports.selectHouses = async function (city, type, tese1, tese2, tese3) {
    const result = await Houses.findAll({ //查询配置，查询条件
        where: {
            entire: {
                // 模糊查询
                [Op.like]: '%' + city + '%' + type + '%' + tese1 + '%' + tese2 + '%' + tese3 + '%'
            }
        },
        raw: true,
        // attributes:['entire']
    }).catch(() => {})
    console.log(result);
    return {
        data: result,
        meta: {
            msg: '查询成功',
            status: '200'
        }
    }
};

// 根据租金查询房源
// exports.selectHousesByZhujin = async function ({
//     city: areaname,
//     min,
//     max
// }) {
//     console.log(areaname, min, max)
//     let result
//     if (max == '') {
//         result = await Houses.findAll({
//             // SELECT * from employee WHERE `name` LIKE '张_' and ismale = 0 and salary >= 12000;
//             where: {
//                 areaname,
//                 price: {
//                     // [Op.overlap]: [5000, 6000],
//                     // [Op.lt]: max,
//                     [Op.gt]: min * 1
//                 }
//             }
//         }).catch(() => {});
//     } else {
//         result = await Houses.findAll({
//             // SELECT * from employee WHERE `name` LIKE '张_' and ismale = 0 and salary >= 12000;
//             where: {
//                 areaname,
//                 price: {
//                     // [Op.overlap]: [5000, 6000],
//                     [Op.lt]: max * 1,
//                     [Op.gt]: min * 1
//                 }
//             }
//         }).catch(() => {});
//     }
//     console.log(result);
//     return {
//         data: result,
//         meta: {
//             msg: '查询成功',
//             status: '200'
//         }
//     }
// };

// // 根据筛选查房源
// exports.selectHousesByShaixuan = async function ({
//     roomType,
//     floor,
//     community,
//     roomTypeID,
//     lineNum,
//     lineId,
//     oriented,
//     tags,
//     communityID,
//     tagsId,
//     size,
//     geduan,
//     year
// }) {
//     let definedObj = {
//         roomType,
//         floor,
//         community,
//         roomTypeID,
//         lineNum,
//         lineId,
//         oriented,
//         tags,
//         communityID,
//         tagsId,
//         size,
//         geduan,
//         year
//     }
//     let reqObj = {}
//     for (let k in definedObj) {
//         if (definedObj[k] != undefined && definedObj[k] != '') {
//             reqObj[k] = definedObj[k]
//         }
//     }
//     console.log(reqObj);
//     // console.log(definedarr);
//     const result = await Houses.findAll({ //查询配置，查询条件
//         where: {
//             ...reqObj,
//         },
//         // raw: true,
//         // attributes:['entire']
//     }).catch(() => {})
//     console.log(result);
//     if (result) {
//         return {
//             data: result,
//             meta: {
//                 msg: '查询成功',
//                 status: '200'
//             }
//         }
//     } else {
//         return {
//             data: {},
//             meta: {
//                 msg: '查询房源失败',
//                 status: '400'
//             }
//         }
//     }
// }

// 筛选房源
exports.selectHousesByShaixuan = async function ({
    city: areaname,
    min,
    max,
    roomType,
    floor,
    community,
    roomTypeID,
    lineNum,
    lineId,
    oriented,
    tags,
    communityID,
    tagsId,
    size,
    geduan,
    year
}) {
    let definedObj = {
        roomType,
        floor,
        community,
        roomTypeID,
        lineNum,
        lineId,
        oriented,
        tags,
        communityID,
        tagsId,
        size,
        geduan,
        year
    }
    let reqObj = {}
    for (let k in definedObj) {
        if (definedObj[k] != undefined && definedObj[k] != '') {
            reqObj[k] = definedObj[k]
        }
    }
    console.log(reqObj);
    // console.log(definedarr);
    let result;
    if (max == '') {
        result = await Houses.findAll({
            // SELECT * from employee WHERE `name` LIKE '张_' and ismale = 0 and salary >= 12000;
            where: {
                areaname,
                price: {
                    // [Op.overlap]: [5000, 6000],
                    // [Op.lt]: max,
                    [Op.gt]: min * 1
                },
                ...reqObj
            }
        }).catch(() => {});
    } else {
        result = await Houses.findAll({
            // SELECT * from employee WHERE `name` LIKE '张_' and ismale = 0 and salary >= 12000;
            where: {
                areaname,
                price: {
                    // [Op.overlap]: [5000, 6000],
                    [Op.lt]: max * 1,
                    [Op.gt]: min * 1
                },
                ...reqObj
            }
        }).catch(() => {});
    }
        console.log(result);
        if (result) {
            return {
                data: result,
                meta: {
                    msg: '查询成功',
                    status: '200'
                }
            }
        } else {
            return {
                data: {},
                meta: {
                    msg: '查询房源失败',
                    status: '400'
                }
            }
        }
    }
